﻿using NewRetail.Application.Components.WeChat.Configurations;
using NewRetail.Application.Components.WeChat.Entity;
using NewRetail.Application.Core;
using NewRetail.Application.Utility;
using System;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace NewRetail.Application.Components.WeChat.Services
{
    #region 微信accesstoken接口服务
    public interface IWeChatAccessToken: IApplicationService
    {
        /// <summary>
        /// 获取accesstoken
        /// </summary>
        /// <param name="appId"></param>
        /// <returns></returns>
        Task<WechatAccessTokenEntity> GetAccessTokenObject(string appId);
        /// <summary>
        /// 设置accesstoken
        /// </summary>
        /// <param name="wechatAccessTokenObject"></param>
        /// <returns></returns>
        Task SetAccessTokenObject(WechatAccessTokenEntity wechatAccessTokenObject);
    }
    #endregion

    #region 微信accesstoken接口服务实现
    public class WeChatAccessToken: ApplicationService, IWeChatAccessToken
    {
        #region field
        private readonly RedisClient _redisClient;
        private readonly StackExchange.Redis.IDatabase _redis;

        #endregion

        #region 构造
        public WeChatAccessToken(RedisClient redisClient)
        {
            _redisClient = redisClient;
            _redis = _redisClient.Connection.GetDatabase(2);
        }
        #endregion

        #region method

        #region 获取accesstoken
        /// <summary>
        /// 获取accesstoken
        /// </summary>
        /// <param name="appId"></param>
        /// <returns></returns>
        public async Task<WechatAccessTokenEntity> GetAccessTokenObject(string appId)
        {
            var keyWechat = CreateWeChatKey(appId);
            //获取redis里面键对应的值
            var keyWechatValue = await _redis.StringGetAsync(keyWechat);
            //转成对象
            var wechatAccessToken = keyWechatValue.IsNullOrEmpty ? null : UtilsHelper.DeserializeObject<WechatAccessTokenEntity>(keyWechatValue.ToString());
            return wechatAccessToken;
        }
        #endregion

        #region 设置accesstoken
        /// <summary>
        /// 设置accesstoken
        /// </summary>
        /// <param name="wechatAccessTokenObject"></param>
        /// <returns></returns>
        public async Task SetAccessTokenObject(WechatAccessTokenEntity wechatAccessTokenObject)
        {
            var keyWechat = CreateWeChatKey(wechatAccessTokenObject.AppId);
            var valueRet = UtilsHelper.SerializeObject<WechatAccessTokenEntity>(wechatAccessTokenObject);
            await _redis.StringSetAsync(key: keyWechat, value: valueRet);//, expiry: TimeSpan.FromHours(1));
        }
        #endregion

        #endregion

        #region 生成key
        /// <summary>
        /// 生成key
        /// </summary>
        /// <param name="appId"></param>
        /// <returns></returns>
        private string CreateWeChatKey(string appId)
        {
            var keyWechat = $"{ConstOptions.KeyWechatRedisPrefix}{appId}";
            return keyWechat;
        }
        #endregion
    }
    #endregion
}
